#region Imported Namespaces
using System;
using System.Collections.Specialized;
using System.Data;
using System.Web.UI.WebControls;

using PHSRAG.Insight21.Context;
using BO = PHSRAG.Insight21.BusinessObjects;
using DataAccess = PHSRAG.Utility.DataAccess;
using ICachePolicy = PHSRAG.Insight21.Policy.ICachePolicy;
using GenerateKey = PHSRAG.Insight21.BusinessObjects.ModuleKey;
#endregion

namespace PHSRAG.Insight21.SearchContext
{
	#region Class PurchasingSearchContext
	/// <summary>
	/// The PurchasingSearchContext class encapsulates the search context associated with the PurchasingSearch control.
	/// </summary>
	public class PurchasingSearchContext : SearchContext
	{
		#region Constants
		private static string PurchasingSearchContextKey = GenerateKey.MakeKey("purchasingSearchContext_BDC44DB7-75C4-4125-B258-012C77C7D204",GenerateKey.Purchasing);
		private const string storeProcName = "GetPurchasingSearchData";		
		#endregion

		#region Private Types
		/// <summary>
		/// The DataTableIndex enumeration defines the index of each table (which holds data pertaining to the set of
		/// drop-down-lists) within the data container.
		/// </summary>
		private enum DataTableIndex
		{
			Category, POAPOrderTypes, ALLTransactionTypes, TransactionTypeValues, TransactionDateTypeValues, SelectOrderViewValues, ExpenseStatusValues, UserOrderTypeValues, Max
		}
		#endregion

		#region Public Properties
		/// <summary>
		/// Get the key to be used in all caching operations for this instance of the object.
		/// </summary>
		public static string CacheKey	{ get { return PurchasingSearchContextKey; } }

		/// <summary>
		/// Get the data table from the data container which contains data for the Category drop-down-list.
		/// </summary>
		public DataTable Category
		{
			get { return GetTable(DataTableIndex.Category); }
		}

		/// <summary>
		/// Get the POAP order types data table from the data container which contains data for the Type drop-down-list.
		/// this will be used for Purchasing search pages
		/// </summary>
		public DataTable POAPOrderTypes
		{
			get { return GetTable(DataTableIndex.POAPOrderTypes); }
		}

		/// <summary>
		/// Get the ALL Transaction Types data table from the data container which contains data for the Type drop-down-list.
		/// this will be used for Transaction Overview to load ordertype dropdown in search control
		/// </summary>
		public DataTable ALLTransactionTypes
		{
			get { return GetTable(DataTableIndex.ALLTransactionTypes); }
		}

		/// <summary>
		/// Get the data table from the data container which contains data for the Transaction Type Values drop-down-list.
		/// </summary>
		public DataTable TransactionTypeValues
		{
			get { return GetTable(DataTableIndex.TransactionTypeValues); }
		}
		
		/// <summary>
		/// Get the data table from the data container which contains data for the Transaction Date Type Values drop-down-list.
		/// </summary>
		public DataTable TransactionDateTypeValues
		{
			get { return GetTable(DataTableIndex.TransactionDateTypeValues); }
		}

		/// <summary>
		/// Get the data table from the data container which contains data for the Select Order view drop-down-list.
		/// </summary>
		public DataTable SelectOrderViewValues
		{
			get { return GetTable(DataTableIndex.SelectOrderViewValues); }
		}
		
		/// <summary>
		/// Get the data table from the data container which contains data for the Expense Status Values drop-down-list.
		/// </summary>
		public DataTable ExpenseStatusValues
		{
			get { return GetTable(DataTableIndex.ExpenseStatusValues); }

		}

		/// <summary>
		/// Get the data table from the data container which contains data for the User Order Type Values drop-down-list.
		/// </summary>
		public DataTable UserOrderTypeValues
		{
			get { return GetTable(DataTableIndex.UserOrderTypeValues); }
		}

		#endregion

		#region Constructor
		/// <summary>
		/// Create an instance of the PurchasingSearchContext class. Since this object does not hold any state (other than
		/// whatever the base class holds), there is nothing to initialize.
		/// </summary>
		/// <param name="cachePolicy">Cache policy to use</param>
		public PurchasingSearchContext(ICachePolicy cachePolicy) : base(cachePolicy)
		{
		}
		#endregion

		#region BusinessObject Methods
		/// <summary>
		/// Cache this instance of the PurchasingSearchContext object using the current cache policy.
		/// </summary>
		public override void Cache()
		{
			CachePolicy.Cache(CacheKey, this);
		}

		/// <summary>
		/// Load agreement search data into the data container. The loading is done by invoking a stored procedure.
		/// </summary>
		/// <param name="connectionString">Connection string to the database</param>
		/// <param name="args">Arugments to the invoked stored procedure</param>
		public override void Load(string connectionString, params object[] args)
		{					
			DataContainer = DataAccess.GetDataSet(connectionString, storeProcName);
			if(DataContainer.Tables.Count != (int)DataTableIndex.Max)
				throw new Exception("Failed to load Purchasing Search Context Data");
		}

		/// <summary>
		/// Remove this instance of the PurchasingSearchContext object from the cache using the current cache policy.
		/// </summary>
		public override void UnCache()
		{
			CachePolicy.UnCache(CacheKey);
		}
		#endregion

		#region SearchContext Members
		/// <summary>
		/// Reset the internal search state.
		/// </summary>
		public override void Reset(string pageName)
		{
			DataContainer.Reset();
		}
		#endregion

		#region Private Methods
		private DataTable GetTable(DataTableIndex index)
		{
			if (DataContainer == null || DataContainer.Tables.Count == 0)
				Load(DataAccess.ConnectionString);
			return DataContainer.Tables[(int)index];
		}
		#endregion
	}
	#endregion
}
// --- EOF ---